app.controller('MyFooterController',['$scope','$location', '$rootScope','globalVariableService','$modal','userService',
    function ($scope, $location, $rootScope, globalVariableService, $modal,userService) {
    globalVariableService.initRootScope();
    getStorageValue('snsEnv');
    getStorageValue('snsApiKey');
    $rootScope.showFooter = true;
    $scope.homePageClick = true;
    $rootScope.friendChatTotalNum = 0;
    $rootScope.isFriendGetUnRead = false;
       /* document.removeEventListener("GET_UNREAD_CALLBACK");
        document.addEventListener("GET_UNREAD_CALLBACK", function (evt) {
            $rootScope.isPending = false;
            if(!$rootScope.isFriendGetUnRead){
                for(var i = 0; i < evt.data.length; ++i){
                    $rootScope.friendChatTotalNum += parseInt(evt.data[i].msgCount);
                }
            }
            $rootScope.$$phase || $rootScope.$apply();
            $scope.$$phase || $scope.$apply();
        }, false);*/
        var unRead = function(evt){
            $rootScope.isPending = false;
            if(!$rootScope.isFriendGetUnRead){
                for(var i = 0; i < evt.data.length; ++i){
                    $rootScope.friendChatTotalNum += parseInt(evt.data[i].msgCount);
                }
            }
            $rootScope.$$phase || $rootScope.$apply();
            $scope.$$phase || $scope.$apply();
        }
        document.removeEventListener("GET_UNREAD_CALLBACK", unRead, false);
        document.addEventListener("GET_UNREAD_CALLBACK", unRead, false);

        userService.getUnReadCount().then(function (data) {
            if(data.status == 0){
                $rootScope.friendChatTotalNum += data.data.count;
            }
            userService.getFriends().then(function (data) {
                if(data.status == 0){
                    var hxUserNames = [];
                    var friends = data.data.friends;
                    for(var item in friends){
                        hxUserNames.push(friends[item].hxUserName);
                    }
                    hxGetUnRead(hxUserNames);
                }
            });
        });
    $scope.clickInit = function()
    {
        $rootScope.homePageClick = false;
        $rootScope.searchPageClick = false;
        $rootScope.friendPageClick = false;
        $rootScope.snsPageClick = false;
        $rootScope.myselfPageClick = false;
    }
    $scope.homePage= function()
    {
        $rootScope.clickInit();
        $rootScope.homePageClick = true;
        $location.path('/consultant/index');
    }
    $scope.searchPage = function()
    {
        $rootScope.clickInit();
        $rootScope.searchPageClick = true;
        var toPath = '/search';
        if(!globalVariableService.checkLogin(toPath))
        {
            return;
        }else{
            userService.getProfile().then(function(data){
                if(data.status == 0) {
                    if (data.data.user_status == 0) {
                        $location.path('/promptRegister').search({backUrl: '/'});
                    }
                    if (data.data.user_status == 1) {
                        $location.path("/promptExamine").search({backUrl: '/'});
                    }
                    if (data.data.user_status == 2) {
                        $location.path(toPath).search({backUrl: '/consultant/index'});
                    }
                    if (data.data.user_status == 3) {
                        $location.path("/promptFeedback").search({backUrl: '/'});
                    }
                }else{
                    var modalInstance = $modal.open({
                        templateUrl: 'views/tmpl_modal_alert.html',
                        controller: ModalAlertCtrl,
                        size: 'sm',
                        resolve: {
                            alertObj: function () {
                                return {message: data.friendlyMessage};
                            }
                        }
                    });
                }
            });
        }
    }
    $scope.friendPage = function()
    {
        $rootScope.clickInit();
        $rootScope.friendPageClick = true;
        var toPath = '/friends';
        if(!globalVariableService.checkLogin(toPath))
        {
            return;
        }else{
            userService.getProfile().then(function(data){
                if(data.status == 0) {
                    if (data.data.user_status == 0) {
                        $location.path('/promptRegister').search({backUrl: '/'});
                    }
                    if (data.data.user_status == 1) {
                        $location.path("/promptExamine").search({backUrl: '/'});
                    }
                    if (data.data.user_status == 2) {
                        $location.path(toPath).search({backUrl: '/consultant/index'});
                    }
                    if (data.data.user_status == 3) {
                        $location.path("/promptFeedback").search({backUrl: '/'});
                    }
                }else{
                    var modalInstance = $modal.open({
                        templateUrl: 'views/tmpl_modal_alert.html',
                        controller: ModalAlertCtrl,
                        size: 'sm',
                        resolve: {
                            alertObj: function () {
                                return {message: data.friendlyMessage};
                            }
                        }
                    });
                }
            });
        }
    }
    $scope.snsPage = function()
    {
        $rootScope.clickInit();
        $rootScope.snsPageClick = true;
        var toPath = '/topicDetails';
        if(!globalVariableService.checkLogin(toPath))
        {
            return;
        }else{
            setExitSNSSiteUrl($location.absUrl());
            userService.getProfile().then(function(data){
                if(data.status == 0) {
                    if (data.data.user_status == 0) {
                        /*modalMessage($modal, '您还没有注册,请您注册');*/
                        $location.path('/promptRegister').search({backUrl: '/'});
                    }
                    if (data.data.user_status == 1) {
                        $location.path("/promptExamine").search({backUrl: '/'});
                    }
                    if (data.data.user_status == 2) {
                        /*modalMessage($modal, '您的资料已通过审核，成为了普通会员');*/
                        location.href = './sns/index.html#/?env='+getSnsEnv()+'&ApiKey='+getSnsApiKey()+'&Token='+getSnsToken();
                    }
                    if (data.data.user_status == 3) {
                       /* modalMessage($modal, '您的资料没有通过审核，原因是有所填内容不符合,请重新注册');*/
                        $location.path("/promptFeedback").search({backUrl: '/'});
                    }
                }else{
                    var modalInstance = $modal.open({
                        templateUrl: 'views/tmpl_modal_alert.html',
                        controller: ModalAlertCtrl,
                        size: 'sm',
                        resolve: {
                            alertObj: function () {
                                return {message: data.friendlyMessage};
                            }
                        }
                    });
                }
            });
        }
    }
    $scope.myselfPage = function()
    {
        $rootScope.clickInit();
        $rootScope.myselfPageClick = true;
        var toPath = '/myPersonInfo';
        if(!globalVariableService.checkLogin(toPath))
        {
            return;
        }else{
            userService.getProfile().then(function(data){
                if(data.status == 0) {
                    if (data.data.user_status == 0) {
                        $location.path('/promptRegister').search({backUrl: '/'});
                    }
                    if (data.data.user_status == 1) {
                        $location.path("/promptExamine").search({backUrl: '/'});
                    }
                    if (data.data.user_status == 2) {
                        $location.path(toPath).search({backUrl: '/consultant/index'});
                    }
                    if (data.data.user_status == 3) {
                        $location.path("/promptFeedback").search({backUrl: '/'});
                    }
                }else{
                    var modalInstance = $modal.open({
                        templateUrl: 'views/tmpl_modal_alert.html',
                        controller: ModalAlertCtrl,
                        size: 'sm',
                        resolve: {
                            alertObj: function () {
                                return {message: data.friendlyMessage};
                            }
                        }
                    });
                }
            });
        }
    }
        /*好友详情 footer */
        $scope.collect = function () {
            var userId = {collectUserId: globalVariableService.userId};
            userService.collect(userId).then(function(data){
                if(data.status == 0){
                    $rootScope.cancelCollection = true;
                    var modalInstance = $modal.open({
                        templateUrl: 'views/tmpl_modal_alert.html',
                        controller: ModalAlertCtrl,
                        size: 'sm',
                        resolve: {
                            alertObj: function () {
                                return {message: "成功添加到收藏"};
                            }
                        }
                    });
                }else{
                    var modalInstance = $modal.open({
                        templateUrl: 'views/tmpl_modal_alert.html',
                        controller: ModalAlertCtrl,
                        size: 'sm',
                        resolve: {
                            alertObj: function () {
                                return {message: data.friendlyMessage};
                            }
                        }
                    });
                }
            })
        }

        $scope.cancelCollect = function () {
            var userId = {collectUserId: globalVariableService.userId};
            userService.cancelCollect(userId).then(function (data) {
                if (data.status == 0) {
                    $rootScope.cancelCollection = false;
                    var modalInstance = $modal.open({
                        templateUrl: 'views/tmpl_modal_alert.html',
                        controller: ModalAlertCtrl,
                        size: 'sm',
                        resolve: {
                            alertObj: function () {
                                return {message: "已取消收藏"};
                            }
                        }
                    });
                }
            })
        };
       /*加好友*/
        $scope.addFriend = function () {
           var applyUserId = {applyUserId: globalVariableService.userId};
         userService.applyFriend(applyUserId).then(function(data){
                if(data.status == 0)
                {
                    var modalInstance = $modal.open({
                        templateUrl: 'views/tmpl_modal_alert.html',
                        controller: ModalAlertCtrl,
                        size: 'sm',
                        resolve: {
                            alertObj: function () {
                                return {message: "好友申请已发出，等待对方同意"};
                            }
                        }
                    });
                }else if(data.status == 600002){
                    $location.path('/integralPrompt').search({backUrl: '/detailsInfo'});
                } else{
                    var modalInstance = $modal.open({
                        templateUrl: 'views/tmpl_modal_alert.html',
                        controller: ModalAlertCtrl,
                        size: 'sm',
                        resolve: {
                            alertObj: function () {
                                return {message: data.friendlyMessage};
                            }
                        }
                    });
                }
            });
        }
        //聊天
        $scope.friendChat = function(){
            var userInfo = globalVariableService.userInfo;
            var toHxUserName = globalVariableService.friendDetailsInfo.hxUserName;
            if(userInfo.userId){
                hxChat(userInfo.hxUserName, toHxUserName);
            } else {
                userService.getProfile().then(function (data) {
                    if(data.status == 0){
                        userInfo = data.data;
                        hxChat(userInfo.hxUserName, toHxUserName);
                    }
                });
            }
        }
        /*我的资料 footer */
        $scope.backModify = function () {
            $location.path(globalVariableService.infoNextRoutes[--globalVariableService.currentInd]).search({backUrl:'/'});
        }

        $scope.nextStep = function () {
            var ind = globalVariableService.currentInd;
            if(ind >= globalVariableService.infoNextRoutes.length-2) {
                var a =  /^1\d{10}$/;
                //不是必填项，可以不输入
                if($rootScope.personInfo.phoneNum && ($rootScope.personInfo.phoneNum.length!= 11 || !$rootScope.personInfo.phoneNum.match(a))){
                    modalMessage($modal, '请输入正确的手机号');
                    return;
                }
                //用户更新详细信息
                var updateInfo = {
                    identity_no: $rootScope.personInfo.identity_no,
                    edu_bg_photo: $rootScope.personInfo.edu_bg_photo,
                    name: $rootScope.personInfo.name,
                    weixin_id: $rootScope.personInfo.weixin_id,
                    birthday_year: $rootScope.personInfo.birthday_year,
                    sex: $rootScope.personInfo.sex.id,
                    constellation: $rootScope.personInfo.constellation.id,
                    hight: $rootScope.personInfo.hight,
                    weight: $rootScope.personInfo.weight,
                    graduate_school: $rootScope.personInfo.graduate_school,
                    education_background: $rootScope.personInfo.education_background.id,
                    characterization: $rootScope.personInfo.characterization,
                    personal_required: $rootScope.personInfo.personal_required,
                    city: $rootScope.personInfo.city.city,
                    professional: $rootScope.personInfo.professional,
                    country: $rootScope.personInfo.country.country,
                    province: $rootScope.personInfo.province.province,
                    phoneNum: $rootScope.personInfo.phoneNum,
                    identity_photo : $rootScope.personInfo.identity_photo
                };
                userService.updateInfo(updateInfo).then(function(data){
                    if(data.status == 0){
                        userService.getProfile().then(function(data){
                            if(data.status == 0){
                                globalVariableService.userInfo = data.data;
                                $rootScope.sexIndex = data.data.sex;
                                $rootScope.constellationIndex = data.data.constellation;
                                $rootScope.education_backgroundIndex = data.data.education_background;
                            }else{
                                var modalInstance = $modal.open({
                                    templateUrl: 'views/tmpl_modal_alert.html',
                                    controller: ModalAlertCtrl,
                                    size: 'sm',
                                    resolve:{
                                        alertObj: function(){
                                            return {message: data.friendlyMessage};
                                        }
                                    }
                                });
                             }
                        });
                        $location.path(globalVariableService.infoNextRoutes[++globalVariableService.currentInd]).search({backUrl:globalVariableService.infoNextRoutes[ind]});
                    }else{
                        var modalInstance = $modal.open({
                            templateUrl: 'views/tmpl_modal_alert.html',
                            controller: ModalAlertCtrl,
                            size: 'sm',
                            resolve:{
                                alertObj: function(){
                                    return {message: data.friendlyMessage};
                                }
                            }
                        });
                    }
                });

            }
            if(ind== 0)
            {
                    if (!$rootScope.personInfo.name) {
                        modalMessage($modal,'请输入真实姓名');
                        return;
                    }
                    if (!$rootScope.personInfo.weixin_id) {
                        modalMessage($modal,'请输入微信号');
                        return;
                    }
                    if ($rootScope.personInfo.constellation == undefined) {

                        modalMessage($modal, '请输入星座');
                        return;
                    }
                    if (!$rootScope.personInfo.hight) {
                        modalMessage($modal, '请输入身高');
                        return;
                    }
                    if (!$rootScope.personInfo.weight) {
                        modalMessage($modal, '请输入体重');
                        return;
                    }
                    if(!$rootScope.personInfo.province){
                        modalMessage($modal, '请输入省份');
                        return;
                    }
                    if(!$rootScope.personInfo.city){
                        modalMessage($modal, '请输入城市');
                        return;
                    }
                    if (!$rootScope.personInfo.professional) {
                        modalMessage($modal, '请输入所在单位职业');
                        return;
                    }
                    if (!$rootScope.personInfo.graduate_school) {
                        modalMessage($modal, '请输入毕业院校');
                        return;
                    }
                    if ($rootScope.personInfo.constellation == undefined) {

                        modalMessage($modal, '请输入学历');
                        return;
                    }
                    $location.path(globalVariableService.infoNextRoutes[++globalVariableService.currentInd]).search({backUrl: globalVariableService.infoNextRoutes[ind]});
            }
            if(ind == 1)
            {
                if(!$rootScope.personInfo.characterization)
                {
                    modalMessage($modal, '请填写个人描述');
                    return;
                }
                if(!$rootScope.personInfo.personal_required)
                {
                    modalMessage($modal, '请填写征友要求');
                    return;
                }
                $location.path(globalVariableService.infoNextRoutes[++globalVariableService.currentInd]).search({backUrl:globalVariableService.infoNextRoutes[ind]});
            }
            if(ind == 2)
            {
                /*var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;*/
                if ($rootScope.personInfo.identity_no==undefined || !$rootScope.personInfo.identity_no) {
                    modalMessage($modal, '请输入身份证件号');
                    return;
                }
                if($scope.personInfo.identity_photo.length == 0){
                    modalMessage($modal, '请上传身份证件照片');
                    return;
                }
                $location.path(globalVariableService.infoNextRoutes[++globalVariableService.currentInd]).search({backUrl:globalVariableService.infoNextRoutes[ind]});
            }
        }
}]);

app.controller('MyPersonInfoController',    ['$scope', '$location',  '$rootScope','globalVariableService','userService', '$modal',
    function($scope, $location, $rootScope,globalVariableService, userService, $modal){
        globalVariableService.initRootScope();
        $rootScope.showBackButton=false;
        $rootScope.pageTitle = "我的";
        $rootScope.showFooter = true;
        $rootScope.canRemoveAction = false;
        $scope.uploadedImage = {};
        $scope.comp = true;
        $scope.my = {};
        $scope.uploadedImages = globalVariableService.ImageList;//图片
        $scope.divWidth = 0;
        var removeImgIndex = 0;
        if(globalVariableService.userInfo.name){
            $scope.uploadedImage.head_url = globalVariableService.userInfo.head_url;
            $scope.my.name = globalVariableService.userInfo.name;
            $scope.sex = globalVariableService.userInfo.sex;
            if( globalVariableService.userInfo.role == 2){
                $scope.my.role = true;
            }else{
                $scope.my.role = false;
            }
            $scope.my.member_no =  globalVariableService.userInfo.member_no;
            $scope.uploadedImages = globalVariableService.userInfo.photos;
             $scope.divWidth = 65*($scope.uploadedImages.length);
            /*for(var i=0 ; i< $scope.uploadedImages.length; i++){
                var image=new Image();
                var imgW=0, imgH= 0, Imgwidth = 0;
                image.src = $scope.uploadedImages[i].url_320_240;
                imgW = image.width;
                imgH = image.height;
                var proportion = imgW / imgH;
                Imgwidth = parseInt(proportion * 60);
                $('imageWH'+ i).css('width', Imgwidth);
                $scope.divWidth += Imgwidth+5;
            }
          $scope.divWidth = $scope.divWidth + 1;*/
        }else{
            userService.getProfile().then(function(data){
                if(data.status == 0){
                    $scope.uploadedImage.head_url = data.data.head_url;
                    $scope.my.name = data.data.name;
                    $scope.sex = data.data.sex;
                    if(data.data.role == 2){
                        $scope.my.role = true;
                        $scope.my.becomeV = true;
                        $scope.my.isVIP = true;
                    }
                    $scope.my.member_no = data.data.member_no;
                    $scope.uploadedImages = data.data.photos;
                    $scope.divWidth = $scope.uploadedImages.length * 65;
                    /*for(var i=0 ; i< $scope.uploadedImages.length; i++){
                        var image=new Image();
                        var imgW=0, imgH= 0, Imgwidth = 0;
                        image.src = $scope.uploadedImages[i].url_320_240;
                        imgW = image.width;
                        imgH = image.height;
                        var proportion = imgW / imgH;
                        Imgwidth = parseInt(proportion * 60);
                        $('imageWH'+ i).css('width', Imgwidth);
                        $scope.divWidth += (Imgwidth+5);
                    }
                   $scope.divWidth = $scope.divWidth + 1;*/
                }else{
                    var modalInstance = $modal.open({
                        templateUrl: 'views/tmpl_modal_alert.html',
                        controller: ModalAlertCtrl,
                        size: 'sm',
                        resolve: {
                            alertObj: function () {
                                return {message: data.friendlyMessage};
                            }
                        }
                    });
                }
            });
        }

        var isUploadAvatar = false;
       /* document.removeEventListener("FILE_UPLOADED_EVENT");
        document.addEventListener("FILE_UPLOADED_EVENT", function (evt) {
            $rootScope.isPending = false;
            alert("FILE_UPLOADED_EVENT");
            $scope.isUploadSuccess = true;
            if(isUploadAvatar){
                $scope.uploadedImage.head_url = evt.data.avatar;
                globalVariableService.userInfo.head_url = $scope.uploadedImage.head_url;
                //$scope.uploadedImage.head_url =  $scope.uploadedImage;
            }else{
                $scope.uploadedImages.push(evt.data);
              *//*  for(var i=0 ; i< $scope.uploadedImages.length; i++){
                    var image=new Image();
                    var imgW=0, imgH= 0, Imgwidth = 0;
                    image.src = $scope.uploadedImages[i].url_320_240;
                    imgW = image.width;
                    imgH = image.height;
                    var proportion = imgW / imgH;
                    Imgwidth = parseInt(proportion * 60);
                    arr.push(Imgwidth);
                    $('imageWH'+ i).css('width', Imgwidth);
                    $scope.divWidth += Imgwidth+5;
                }
                $scope.divWidth = $scope.divWidth + 1;*//*
                $scope.divWidth = 65*($scope.uploadedImages.length);
                globalVariableService.ImageList = $scope.uploadedImages;
            }
            $rootScope.$$phase || $rootScope.$apply();
            $scope.$$phase || $scope.$apply();
        }, false);*/
        var upLoadhand = function(evt){
            $rootScope.isPending = false;
            $scope.isUploadSuccess = true;
            if(isUploadAvatar){
                $scope.uploadedImage.head_url = evt.data.avatar;
                globalVariableService.userInfo.head_url = $scope.uploadedImage.head_url;
            }else{
                $scope.uploadedImages.push(evt.data);
            $scope.divWidth = 65*($scope.uploadedImages.length);
            globalVariableService.ImageList = $scope.uploadedImages;
         }
            $rootScope.$$phase || $rootScope.$apply();
            $scope.$$phase || $scope.$apply();
        }
        $scope.$on("$destroy",function(){
            document.removeEventListener("FILE_UPLOADED_EVENT", upLoadhand, false);
        });
        document.addEventListener("FILE_UPLOADED_EVENT", upLoadhand, false);

        /*document.removeEventListener("FILE_UPLOAD_CANCEL_EVENT");
         document.addEventListener("FILE_UPLOAD_CANCEL_EVENT", function (evt) {
         $rootScope.isPending = false;
         $rootScope.$$phase || $rootScope.$apply();
         }, false);*/
        var cancelHand = function(evt){
            $rootScope.isPending = false;
            $rootScope.$$phase || $rootScope.$apply();
        }
        $scope.$on("$destroy",function(){
            document.removeEventListener("FILE_UPLOAD_CANCEL_EVENT", cancelHand, false);
        });
        document.addEventListener("FILE_UPLOAD_CANCEL_EVENT", cancelHand, false);

        $scope.uploadPhote =function()
        {
            isUploadAvatar = false;
            globalVariableService.uploadImage($scope, 2);
        }

        $scope.$on('removePhotoCallback', function (event, data){
            var params = $scope.uploadedImages[removeImgIndex];
            userService.deletePhoto(params).then(function(data){
                if(data.status == 0){
                    $scope.uploadedImages.splice(removeImgIndex, 1);
                    $scope.divWidth = $scope.uploadedImages.length * 65;
                } else{
                    var modalInstance = $modal.open({
                        templateUrl: 'views/tmpl_modal_alert.html',
                        controller: ModalAlertCtrl,
                        size: 'sm',
                        resolve: {
                            alertObj: function () {
                                return {message: data.friendlyMessage};
                            }
                        }
                    });
                }
            });
        });

        $scope.removePhoto =function(){
            $scope.showRemoveIco = !$scope.showRemoveIco;
            $scope.comp = !$scope.comp;
            $scope.removeImage = function (index) {
                removeImgIndex = index;
                var modalInstance = $modal.open({
                    templateUrl: 'views/tmpl_modal_common_confirm.html',
                    controller: ModalCommonMsgCtrl,
                    size: 'sm',
                    resolve: {
                        obj: function () {
                            return {message:"确定要删除此照片吗？",scope:$scope, callback:"removePhotoCallback"};
                        }
                    }
                });
            };
        }

        $scope.myheadImg = function()
        {
            isUploadAvatar = true;
            globalVariableService.uploadImage($scope, 1);
        }

        var imgs = [];
        angular.forEach($scope.uploadedImages, function (obj) {
            imgs.push(obj.fileUploadId);
        });

        //获取用户是不是会员
        userService.getRole().then(function(data){
            if(data.status == 0){
                if(data.data.role == 1){
                    $scope.my.role = false;
                    $scope.my.becomeV = false;
                }
                if(data.data.role == 2){
                    $scope.my.role = true;
                    $scope.my.becomeV = true;
                    $scope.my.isVIP = true;
                }
            }else{
                var modalInstance = $modal.open({
                    templateUrl: 'views/tmpl_modal_alert.html',
                    controller: ModalAlertCtrl,
                    size: 'sm',
                    resolve: {
                        alertObj: function () {
                            return {message: data.friendlyMessage};
                        }
                    }
                });
            }
        });

        $scope.becomeVIP = function () {
            if($scope.my.isVIP)
            {
                $location.path('/alreadyVIP').search({backUrl:'/myPersonInfo'});
            }else{
                $location.path('/becomeVIP').search({backUrl:'/myPersonInfo'});
            }
        }
        $scope.myInfo = function()
        {
            $location.path('/myPersonInfo/myInfoDetails').search({backUrl: '/myPersonInfo'});
        }
        $scope.myState = function()
        {
            $location.path('/myPersonInfo/myState').search({backUrl: '/myPersonInfo'});
        }
        /*document.removeEventListener("GET_VERSION_CALLBACK");
        document.addEventListener("GET_VERSION_CALLBACK", function (evt) {
            $rootScope.isPending = false;
            alert("GET_VERSION_CALLBACK");
            $rootScope.versionNum = evt.data.version;
            $location.path('/myPersonInfo/myAbout').search({backUrl: '/myPersonInfo'});
            $rootScope.$$phase || $rootScope.$apply();
            $scope.$$phase || $scope.$apply();
        }, false);*/

        var hand = function(evt){
            $rootScope.isPending = false;
            $rootScope.versionNum = evt.data.version;
            $location.path('/myPersonInfo/myAbout').search({backUrl: '/myPersonInfo'});
            $rootScope.$$phase || $rootScope.$apply();
            $scope.$$phase || $scope.$apply();
        }
        $scope.$on("$destroy",function(){
            document.removeEventListener("GET_VERSION_CALLBACK", hand, false);
        });
        document.addEventListener("GET_VERSION_CALLBACK", hand, false);

        $scope.fsAbout = function()
        {
            getAppVersion();
        }
        $scope.privilege = function()
        {
            //获取用户是不是会员
            userService.getRole().then(function(data){
                if(data.status == 0){
                    if(data.data.role == 2){
                        $location.path('/alreadyVIP').search({backUrl: '/myPersonInfo'});
                    }else{
                        $location.path('/becomeVIP').search({backUrl: '/myPersonInfo'});
                    }
                }else{
                    var modalInstance = $modal.open({
                        templateUrl: 'views/tmpl_modal_alert.html',
                        controller: ModalAlertCtrl,
                        size: 'sm',
                        resolve: {
                            alertObj: function () {
                                return {message: data.friendlyMessage};
                            }
                        }
                    });
                }
            });
        }
        $scope.myIntegral = function()
        {
            $location.path('/myPersonInfo/integral').search({backUrl: '/myPersonInfo'});
        }
        $scope.mySns = function(){
            localStorage.setItem('snsKey', location.href);//location.href获取点击当前页面的url，
            location.href = './sns/index.html#/myforum?snsEnv='+getStorageValue('snsEnv')+'&snsApiKey='+getStorageValue('snsApiKey')+'&snsToken='+getStorageValue('snsToken')
        }
        $scope.changePassword = function()
        {
            $location.path('/changePassword').search({backUrl: '/myPersonInfo'})
        }
        $scope.myCollection = function(){
            $location.path('/collect').search({backUrl:'/myPersonInfo'});
        }

        $scope.loginOut = function()
        {
            if(globalVariableService.isLogin())
            {
                userService.logout().then(function(data){
                    if(data.status == 0)
                    {
                        setAuthToken(null);
                        $location.path("/");
                    }else{
                        var modalInstance = $modal.open({
                            templateUrl: 'views/tmpl_modal_alert.html',
                            controller: ModalAlertCtrl,
                            size: 'sm',
                            resolve: {
                                alertObj: function () {
                                    return {message: data.friendlyMessage};
                                }
                            }
                        });
                    }
                });
            }else{
                var modalInstance = $modal.open({
                    templateUrl: 'views/tmpl_modal_alert.html',
                    controller: ModalAlertCtrl,
                    size: 'sm',
                    resolve: {
                        alertObj: function () {
                            return {message: "您还没有登录"};
                        }
                    }
                });
            }
        }
        var inc = 0;

        $scope.showImage = function(index,$event){
            var photoUrls = [];
            for(var i = $scope.uploadedImages.length - 1; i >= 0; --i){
                photoUrls.push($scope.uploadedImages[i].url_320_240);
            }
            var evt = $event;
            if (evt.stopPropagation) evt.stopPropagation();
            if (evt.preventDefault) evt.preventDefault();
            evt.cancelBubble = true;
            evt.returnValue = false;
            index = photoUrls.length - index - 1;
            if(iOS7()){
                $rootScope.$emit("SHOW_IMAGE_SLIDER_EVENT", {index:index, images:photoUrls});
            }else{
                if(inc>0)
                    setTimeout(function(){
                        $rootScope.$emit("SHOW_IMAGE_SLIDER_EVENT", {index:index, images:photoUrls});
                    },10)
                else
                    $rootScope.$emit("SHOW_IMAGE_SLIDER_EVENT", {index:index, images:photoUrls});
                inc++
            }

        };
    }]);

app.controller('MyInfoController', ['$scope', '$rootScope', '$routeParams', '$location','globalVariableService','$modal','userService',
    function($scope, $rootScope, $routeParams, $location,globalVariableService,$modal, userService){
        $rootScope.pageTitle="我的资料";
        globalVariableService.initRootScope();
        globalVariableService.currentInd = 0;
        $rootScope.showFooter = true;
        $rootScope.isFirst = true;
        $rootScope.glbFooterType = glbFooterType.myInfoNext;
        $routeParams.backUrl = '/myPersonInfo';
        var year = [];
        if(!$rootScope.personInfo){
            $rootScope.personInfo={};
        }
        for(var i = 1930; i <= 2015; i++){
           year.push(i);
        }
        $scope.year = year;
        if($rootScope.personInfo.birthday_year == undefined){
            $rootScope.personInfo.birthday_year = year[60];
        }

        $scope.gender = gender;
        if($rootScope.personInfo.sex == undefined)
        {
            $rootScope.personInfo.sex = gender[0];
        }else{
            if($rootScope.personInfo.sex.gen){
                $rootScope.personInfo.sex = gender[$rootScope.personInfo.sex.id-1];
            }else{
                $rootScope.personInfo.sex = gender[$rootScope.personInfo.sex-1];
            }
        }

        $scope.conste = conste;
        if($rootScope.personInfo.constellation == undefined)
        {
            $rootScope.personInfo.constellation = conste[0];
        }else{
            if($rootScope.personInfo.constellation.name){
                $rootScope.personInfo.constellation = conste[$rootScope.personInfo.constellation.id];
            }else{
                $rootScope.personInfo.constellation = conste[$rootScope.personInfo.constellation];
            }
        }

        //获取国家
            userService.getCountry().then(function(data){
                if(data.status == 0){
                    $scope.countryArray = data.data.countrys;
                    if(!$rootScope.personInfo.country){
                        $rootScope.personInfo.country = data.data.countrys[0];
                    }else{
                        for(var i =0; i< $scope.countryArray.length; i++){
                            if($scope.countryArray[i].country==$rootScope.personInfo.country||$scope.countryArray[i].country==$rootScope.personInfo.country.country){
                                $rootScope.personInfo.country=$scope.countryArray[i];
                                break;
                            }
                        }
                    }
                }else{
                    var modalInstance = $modal.open({
                        templateUrl: 'views/tmpl_modal_alert.html',
                        controller: ModalAlertCtrl,
                        size: 'sm',
                        resolve: {
                            alertObj: function () {
                                return {message: data.friendlyMessage};
                            }
                        }
                    });
                }
            });



            //获取省份
            userService.getProvince().then(function(data){
                if(data.status == 0){
                    $scope.provinceArray = data.data.provinces;
                    if(!$scope.personInfo.province){
                        $scope.personInfo.province = data.data.provinces[0];
                    } else {
                        for(var i =0; i< $scope.provinceArray.length; i++){
                            if($scope.provinceArray[i].province==$rootScope.personInfo.province || $scope.provinceArray[i].province==$rootScope.personInfo.province.province){
                                $rootScope.personInfo.province=$scope.provinceArray[i];
                                break;
                            }
                        }
                    }

                    //获取市区
                    var provinceId = {provinceId: $scope.personInfo.province.provinceId};
                    userService.getCity(provinceId).then(function(data){
                        if(data.status == 0){
                            $scope.cityArray = data.data.citys;
                            $scope.personInfo.city = data.data.citys[0];
                            if(!$scope.personInfo.city){
                                $scope.personInfo.city = data.data.citys[0];
                            } else {
                                for(var i =0; i< $scope.cityArray.length; i++){
                                    if($scope.provinceArray[i].city==$rootScope.personInfo.city){
                                        $rootScope.personInfo.city=$scope.cityArray[i];
                                        break;
                                    }
                                }
                            }
                        }else{
                            var modalInstance = $modal.open({
                                templateUrl: 'views/tmpl_modal_alert.html',
                                controller: ModalAlertCtrl,
                                size: 'sm',
                                resolve: {
                                    alertObj: function () {
                                        return {message: data.friendlyMessage};
                                    }
                                }
                            });
                        }
                    });
                }else{
                    var modalInstance = $modal.open({
                        templateUrl: 'views/tmpl_modal_alert.html',
                        controller: ModalAlertCtrl,
                        size: 'sm',
                        resolve: {
                            alertObj: function () {
                                return {message: data.friendlyMessage};
                            }
                        }
                    });
                }
            });

        var provinceId = {provinceId: ""};
        $scope.sleProvince = function(province)
        {
            provinceId.provinceId = province.provinceId;
            //获取市区
            userService.getCity(provinceId).then(function(data){
                if(data.status == 0){
                    $scope.cityArray = data.data.citys;
                    $scope.personInfo.city = data.data.citys[0];
                }else{
                    var modalInstance = $modal.open({
                        templateUrl: 'views/tmpl_modal_alert.html',
                        controller: ModalAlertCtrl,
                        size: 'sm',
                        resolve: {
                            alertObj: function () {
                                return {message: data.friendlyMessage};
                            }
                        }
                    });
                }
            });
        }

        $scope.education = education;
        if($rootScope.personInfo.education_background == undefined){
            $rootScope.personInfo.education_background = education[0];
        }else{
            if($rootScope.personInfo.education_background.name){
                $rootScope.personInfo.education_background = education[$rootScope.personInfo.education_background.id];
            }else{
                $rootScope.personInfo.education_background = education[$rootScope.personInfo.education_background];
            }

        }
    }]);

app.controller('MyInfoDetailsController', ['$scope', '$rootScope', '$routeParams', '$location','globalVariableService','userService','$modal',
    function($scope, $rootScope, $routeParams, $location,globalVariableService, userService,$modal) {
        globalVariableService.initRootScope();
        $rootScope.pageTitle="我的信息";
        $scope.details = {};
        $scope.details.identity_photo = [];
        userService.getProfile().then(function(data){
           if(data.status == 0){
               globalVariableService.userInfo = data.data;
               $rootScope.sexIndex = data.data.sex;
               $rootScope.constellationIndex = data.data.constellation;
               $rootScope.education_backgroundIndex = data.data.education_background;
               //$scope.details = globalVariableService.userInfo;
               $scope.details = JSON.parse(JSON.stringify(globalVariableService.userInfo));
               if($scope.details.dating_status==1){
                   $scope.details.dating_status = "单身";
               }else if($scope.details.dating_status==2){
                   $scope.details.dating_status = "恋爱中";
               }else if($scope.details.dating_status==3){
                   $scope.details.dating_status = "已婚";
               }
               $scope.details.sex = gender[$rootScope.sexIndex-1].gen;
               $scope.details.constellation = conste[$rootScope.constellationIndex].name;
               $scope.details.education_background = education[$rootScope.education_backgroundIndex].name;
               $scope.details.identity_photo = data.data.identity_photo;
               $scope.details.edu_bg_photo = data.data.edu_bg_photo;
               //国家选中的不是中国就隐藏省份和市区
              /* if($scope.details.country != "中国"){
                   document.getElementById('cityId').style.display = "none";
               }else{
                   document.getElementById('cityId').style.display = "block";
               }*/
           }else{
               var modalInstance = $modal.open({
                   templateUrl: 'views/tmpl_modal_alert.html',
                   controller: ModalAlertCtrl,
                   size: 'sm',
                   resolve: {
                       alertObj: function () {
                           return {message: data.friendlyMessage};
                       }
                   }
               });
           }
        });

        $scope.$on('reviseCallback', function (event, data){
            $rootScope.personInfo = globalVariableService.userInfo;
            $location.path('/myInfoDetails/myInfo').search({backUrl:'/myPersonInfo/myInfoDetails'})
        });
        $scope.revise = function()
        {
            var modalInstance = $modal.open({
                templateUrl: 'views/tmpl_modal_common_confirm.html',
                controller: ModalCommonMsgCtrl,
                size: 'sm',
                resolve: {
                    obj: function () {
                        return {message:"修改资料需重新审核，预计审核时间24小时内。",scope:$scope,callback:"reviseCallback"};
                    }
                }
            });
        }
    }]);

app.controller('MyStateInfoController', ['$scope', '$rootScope', '$location', 'globalVariableService','userService','$modal',
    function($scope, $rootScope, $location, globalVariableService, userService, $modal){
        $rootScope.pageTitle="我的状态";
        globalVariableService.initRootScope();
        $scope.myStatus = {};
        $scope.updateStatus = function(type)
        {
            $scope.myStatus.state = type;
            if(type == 1){
                $scope.myStatus.online = false;
            }
        }
        $scope.switchOnline = function(type)
        {
            if($scope.myStatus.state == type){
                $scope.myStatus.online = !$scope.myStatus.online;
            }
        }

        userService.getProfile().then(function(data){
            if(data.status == 0){
                $scope.myStatus.state  = data.data.dating_status;
                if(data.data.online == 0){
                    $scope.myStatus.online = false;
                }else{
                    $scope.myStatus.online = true;
                }
            }
        })

        $scope.updateToStatus = function(){
            //修改交友状态
            var statusInfo = {datingStatus: $scope.myStatus.state, online: $scope.myStatus.online};
            userService.updateDatingStatus(statusInfo).then(function(data){
                if(data.status == 0){
                    $location.path("myPersonInfo");
                }else{
                    var modalInstance = $modal.open({
                        templateUrl: 'views/tmpl_modal_alert.html',
                        controller: ModalAlertCtrl,
                        size: 'sm',
                        resolve: {
                            alertObj: function () {
                                return {message: data.friendlyMessage};
                            }
                        }
                    });
                }
            });
        }
    }]);

app.controller('MyCollectionInfoController', ['$scope', '$rootScope', '$location','globalVariableService',
    function($scope, $rootScope, $location,globalVariableService){
        $rootScope.pageTitle="我的收藏";
        globalVariableService.initRootScope();
        $scope.myCollection =
         [
            {id:0, avatar:'img/friendAvatar.png', name:'地方化', province:'上海', constellation:'白羊座'},
            {id:0, avatar:'img/friendAvatar.png', name:'地方化', province:'上海', constellation:'白羊座'},
            {id:0, avatar:'img/friendAvatar.png', name:'地方化', province:'上海', constellation:'白羊座'}
        ];
        $scope.details  = function($index)
        {
            $location.path("/detailsInfo").search({backUri:"/friends"});
        }
    }]);

app.controller('MyAboutInfoController', ['$scope', '$rootScope', '$location','globalVariableService',
    function($scope, $rootScope, $location,globalVariableService){
        $rootScope.pageTitle="关于";
        globalVariableService.initRootScope();
        $scope.about = function()
        {
            $location.path('/myPersonInfo/myAbout/about').search({backUrl: "/myPersonInfo/myAbout"});
        }
        $scope.myHair = function()
        {
            $location.path('/myPersonInfo/myAbout/agreement').search({backUrl: "/myPersonInfo/myAbout"});
        }

    }]);

app.controller('MyFriendsController', ['$scope', '$rootScope', '$location','globalVariableService','userService','$modal',
    function($scope, $rootScope, $location, globalVariableService,userService, $modal){
        globalVariableService.initRootScope();
        $rootScope.pageTitle="朋友";
        $rootScope.showFooter=true;
        $rootScope.showBackButton = false;
        $rootScope.isPending = false;
        $scope.friends = [];
        $rootScope.isFriendGetUnRead = true;

       /* document.removeEventListener("HANDLE_PUSH_CALLBACK");
        document.addEventListener("HANDLE_PUSH_CALLBACK", function (evt) {
            $rootScope.isPending = false;
            globalVariableService.userId = evt.data.userid;
            // JSON.stringify(evt.data);
            $location.path('/detailsInfo');
            $rootScope.$$phase || $rootScope.$apply();
            $scope.$$phase || $scope.$apply();
        }, false);*/
        var pushHand = function(evt){
            $rootScope.isPending = false;
            globalVariableService.userId = evt.data.userid;
            // JSON.stringify(evt.data);
            $location.path('/detailsInfo');
            $rootScope.$$phase || $rootScope.$apply();
            $scope.$$phase || $scope.$apply();
        }
        $scope.$on("$destroy",function(){
            document.removeEventListener("HANDLE_PUSH_CALLBACK", pushHand, false);
        });
        document.addEventListener("HANDLE_PUSH_CALLBACK", pushHand, false);

        /*document.removeEventListener("HANDLE_HXPUSH_CALLBACK");
         document.addEventListener("HANDLE_HXPUSH_CALLBACK", function (evt) {
         $rootScope.isPending = false;
         alert("HANDLE_HXPUSH_CALLBACK");
         $location.path('/friends');
         $rootScope.$$phase || $rootScope.$apply();
         $scope.$$phase || $scope.$apply();
         }, false);*/
        var hxPushHand = function(evt){
            $rootScope.isPending = false;
            $location.path('/friends');
            $rootScope.$$phase || $rootScope.$apply();
            $scope.$$phase || $scope.$apply();
        }
        $scope.$on("$destroy",function(){
            document.removeEventListener("HANDLE_HXPUSH_CALLBACK", hxPushHand, false);
        });
        document.addEventListener("HANDLE_HXPUSH_CALLBACK", hxPushHand, false);

        //获取好友请求未读数
        userService.getUnReadCount().then(function(data){
            if(data.status == 0){
                $scope.friendApplyNum = data.data.count;
                $scope.ids = data.data.ids;
            }
        });

        //获取好友列表
        userService.getFriends().then(function(data){
            if(data.status == 0){
                var hxUserNames = [];
                if(data.data.friends && data.data.friends.length > 0){
                    $scope.friends = data.data.friends;
                    for(var item in $scope.friends){
                        var req = $scope.friends[item].constellation;
                        hxUserNames.push($scope.friends[item].hxUserName);
                        if(req!=null)
                        {
                            $scope.friends[item].constellation = conste[req].name;
                        }
                    }
                    hxGetUnRead(hxUserNames);
                }
            }else{
                var modalInstance = $modal.open({
                    templateUrl: 'views/tmpl_modal_alert.html',
                    controller: ModalAlertCtrl,
                    size: 'sm',
                    resolve: {
                        alertObj: function () {
                            return {message: data.friendlyMessage};
                        }
                    }
                });
            }
        });
       /* document.removeEventListener("GET_UNREAD_CALLBACK");
        document.addEventListener("GET_UNREAD_CALLBACK", function (evt) {
            $rootScope.isPending = false;
            alert("GET_UNREAD_CALLBACK");
            for(var i = 0; i < evt.data.length; ++i){
                for(var j = 0; j < $scope.friends.length; ++j){
                    if(evt.data[i].hxUserName == $scope.friends[j].hxUserName){
                       $scope.friends[j].messageCount = evt.data[i].msgCount;
                        break;
                    }
                }
            }
            //根据消息数做逆序
             $scope.friends = _.sortBy($scope.friends,
                function(friend) {
                return - friend.messageCount;
            });
            $rootScope.$$phase || $rootScope.$apply();
            $scope.$$phase || $scope.$apply();
        }, false);*/
        var hand = function(evt){
            $rootScope.isPending = false;
			var data = evt.data;
			if(browser.versions.android)
            {
                data = JSON.parse(evt.data);//转换成json格式
            }
            for(var i = 0; i < data.length; ++i){
                for(var j = 0; j < $scope.friends.length; ++j){
                    if(data[i].hxUserName == $scope.friends[j].hxUserName){
                        $scope.friends[j].messageCount = data[i].msgCount;
                        break;
                    }
                }
            }
            //根据消息数做逆序
            $scope.friends = _.sortBy($scope.friends,
                function(friend) {
                    return - friend.messageCount;
                });
            $rootScope.$$phase || $rootScope.$apply();
            $scope.$$phase || $scope.$apply();
        }
        $scope.$on("$destroy",function(){
            document.removeEventListener("GET_UNREAD_CALLBACK", hand, false);
        });
        document.addEventListener("GET_UNREAD_CALLBACK", hand, false);

        $scope.checkDetails = function(index)
        {
            globalVariableService.userId = $scope.friends[index].user_id;
            $location.path("/detailsInfo").search({backUrl:"/friends"});
        }
        $scope.hxCom = function(index)
        {
            $rootScope.friendChatTotalNum -= $scope.friends[index].messageCount;
            $scope.friends[index].messageCount = 0;
            var userInfo = globalVariableService.userInfo;
            var toHxUserName = $scope.friends[index].hxUserName;
            if(userInfo.userId){
                hxChat(userInfo.hxUserName, toHxUserName);
            } else {
                userService.getProfile().then(function (data) {
                    if(data.status == 0){
                        userInfo = data.data;
                        hxChat(userInfo.hxUserName, toHxUserName);
                    }
                });
            }

        }
        $scope.newFriends = function () {
            //清除好友申请数
            var ids = {ids: $scope.ids};
            $rootScope.friendChatTotalNum -= $scope.friendApplyNum;
            userService.clearUnRead(ids);
            $location.path('/newFriends').search({backUrl: '/friends'});
        }
    }]);

app.controller('SearchController', ['$scope', '$rootScope', '$location','globalVariableService','userService','$modal',
    function($scope, $rootScope, $location, globalVariableService, userService, $modal){
        globalVariableService.initRootScope();
        $rootScope.pageTitle = '搜索';
        $rootScope.showBackButton = false;
        $rootScope.showFooter=true;
        $rootScope.everySearchCount = true;
        $scope.search = [];
        //获取每日推荐
        userService.getRecommend().then(function(data){
            if(data.status == 0){
              $scope.friends  = data.data.friends;
                for(var item in $scope.friends){
                    var req = $scope.friends[item].constellation;
                    if(req!=null)
                    {
                        $scope.friends[item].constellation = conste[req].name;
                    }
                }
            }else{
                var modalInstance = $modal.open({
                    templateUrl: 'views/tmpl_modal_alert.html',
                    controller: ModalAlertCtrl,
                    size: 'sm',
                    resolve: {
                        alertObj: function () {
                            return {message: data.friendlyMessage};
                        }
                    }
                });
            }
        })
        $scope.checkDetails = function(index){
            globalVariableService.userId = $scope.friends[index].userId;
            $location.path("/detailsInfo").search({backUrl:"/search"});
        }
        //获取国家
        userService.getCountry().then(function(data){
            if(data.status == 0){
                $scope.countryArray = data.data.countrys;
                $scope.countryArray.splice(0, 0, {country:'全部', countryId: -1});
                $scope.search.country = $scope.countryArray[0];
            }else{
                var modalInstance = $modal.open({
                    templateUrl: 'views/tmpl_modal_alert.html',
                    controller: ModalAlertCtrl,
                    size: 'sm',
                    resolve: {
                        alertObj: function () {
                            return {message: data.friendlyMessage};
                        }
                    }
                });
            }
        });
        //获取省份
        userService.getProvince().then(function(data){
            if(data.status == 0){
                $scope.provinceArray = data.data.provinces;
                $scope.search.province = data.data.provinces[0];
            }else{
                var modalInstance = $modal.open({
                    templateUrl: 'views/tmpl_modal_alert.html',
                    controller: ModalAlertCtrl,
                    size: 'sm',
                    resolve: {
                        alertObj: function () {
                            return {message: data.friendlyMessage};
                        }
                    }
                });
            }
        });

        var birth = [];
        for(var i = 1967;i<=1996;i++){
            birth.push(i);
        }
        $scope.birth = birth;
        $scope.search.startYear = birth[0];
        $scope.search.endYear = birth[birth.length-1];

        var height = [];
        for(var i = 155;i<=195;i++){
            height.push(i);
        }
        $scope.height = height;
        $scope.search.startHeight = height[0];
        $scope.search.endHeight = height[height.length-1];

        //如果有FashionSingles ID就按ID搜索否则
        $scope.searchFS = function ()
        {
            var search = $scope.search;
            var searchInfo = {startYear: search.startYear, endYear: search.endYear, startHeight:search.startHeight,endHeight:search.endHeight, memberno:search.memberno};
            searchInfo.country = search.country.country;
            if(searchInfo.country == '全部'){
                searchInfo.country = null;
                searchInfo.province = null;
            }else{
                if(search.province == undefined){
                    searchInfo.province = null;
                }else{
                    searchInfo.province = search.province.province;
                }
            }
            if(searchInfo.memberno){
                $rootScope.everySearchCount = false;
            }
            userService.searchFriends(searchInfo).then(function(data){
                    if(data.status == 0){
                        if(!data.data|| data.data.friends.length == 0){
                            var modalInstance = $modal.open({
                                templateUrl: 'views/tmpl_modal_alert.html',
                                controller: ModalAlertCtrl,
                                size: 'sm',
                                resolve: {
                                    alertObj: function (){
                                        return {message: "暂无满足您条件会员，请重新设置条件搜索"};
                                    }
                                }
                            });
                        }else{
                            globalVariableService.friendsArray  = data.data.friends;
                            $location.path('/searchFriends').search({backUrl: '/search'});
                        }
                    }else if(data.status == 600002){
                        $location.path('/integralPrompt').search({backUrl: '/search'});
                    }else if(data.status == 500000){
                        $location.path('/searchCountFull').search({backUrl: '/search'});
                    } else{
                        var modalInstance = $modal.open({
                            templateUrl: 'views/tmpl_modal_alert.html',
                            controller: ModalAlertCtrl,
                            size: 'sm',
                            resolve: {
                                alertObj: function () {
                                    return {message: data.friendlyMessage};
                                }
                            }
                        });
                    }
                });
        };
    }]);

app.controller('DetailsInfoController', ['$scope','$anchorScroll', '$routeParams', '$rootScope', '$location', 'globalVariableService','userService','$modal',
    function($scope, $anchorScroll,$routeParams, $rootScope, $location,globalVariableService,userService, $modal){
        globalVariableService.initRootScope();
        $rootScope.glbFooterType = glbFooterType.friendDetail;
        $rootScope.pageTitle="详情";
        $rootScope.showQuit = false;
        $rootScope.showFooter = true;
        $rootScope.addFriends = true;
        $routeParams.backUrl = '/friends';
        $scope.friend = {};
        $scope.photos = [];
        $scope.divWidth = 0;
        //var userId = getParameterFromUrl("user_id");
        //获取指定用户的详情信息
        var userid = {userid: globalVariableService.userId};
        userService.getProfileById(userid).then(function(data){
            if(data.status == 0){
                $scope.photos = data.data.photos;
               $scope.friend = data.data;
                if(data.data.sex == 1){
                    $scope.friend.sex = gender[data.data.sex-1].gen;
                }else{
                    $scope.friend.sex = gender[data.data.sex-1].gen;
                }
                //查看是不是收藏了，收藏了按钮就变成取消收藏
               if(data.data.isCollect == true){
                    $rootScope.cancelCollection =true;

               }else{
                   $rootScope.cancelCollection = false;
               }
                //查看是不是好友，已是好友则删除图标隐掉
                if(data.data.isFriend == true){
                    $rootScope.showQuit = true;
                    $rootScope.addFriends = false;
                    $rootScope.chat = true;
                }
                if($scope.friend.dating_status==1){
                   $scope.friend.datingStatus = "单身";
                }else if($scope.friend.dating_status==2){
                   $scope.friend.datingStatus = "恋爱中";
                }else if($scope.friend.dating_status==3){
                   $scope.friend.datingStatus = "已婚";
                }
                globalVariableService.userId = data.data.userId;
             /*  globalVariableService.friendDetailsInfo.user_id = data.data.userId;*/
               globalVariableService.friendDetailsInfo.hxUserName = data.data.hxUserName;
               $scope.divWidth = $scope.photos.length * 65;
               var constellationIndex = data.data.constellation;
               var education_backgroundIndex = data.data.education_background;
               $scope.friend.constellation = conste[constellationIndex].name;
               $scope.friend.education_background = education[education_backgroundIndex].name;
               /* for(var i = $scope.photos.length - 1; i >= 0; --i){
                    photoUrls.push($scope.photos[i].url_320_240);
                    var image=new Image();
                    var imgW=0, imgH= 0, Imgwidth = 0;
                    image.src = $scope.photos[i].url_320_240;
                    imgW = image.width;
                    imgH = image.height;
                    var proportion = imgW / imgH;
                    Imgwidth = parseInt(proportion * 60);
                    $('upWH'+ i).css('width', Imgwidth);
                    $scope.divWidth += Imgwidth+5;
                }*/
            }else{
                var modalInstance = $modal.open({
                    templateUrl: 'views/tmpl_modal_alert.html',
                    controller: ModalAlertCtrl,
                    size: 'sm',
                    resolve: {
                        alertObj: function () {
                            return {message: data.friendlyMessage};
                        }
                    }
                });
            }
        });

        $scope.cancelDelete = function(){
            $rootScope.removePopover = false;
        }

        $scope.PayVIP = function(){
            $location.path('/becomeVIP').search({backUrl: '/detailsInfo'});
        }

        userService.getProfile().then(function(data){
            if(data.status == 0){
                if(data.data.role == 2){
                    $scope.isVIP=true;
                }else{
                    $scope.updateVIP = true;
                }
            }
        });
        var inc = 0;
        $scope.showImage = function(index,$event){
            var photoUrls = [];
            var evt = $event;
            for(var i = $scope.photos.length - 1; i >= 0; --i){
                photoUrls.push($scope.photos[i].url_320_240);
            }
            if (evt.stopPropagation) evt.stopPropagation();
            if (evt.preventDefault) evt.preventDefault();
            evt.cancelBubble = true;
            evt.returnValue = false;
            index = photoUrls.length - index - 1;
            if(iOS7()){
                $rootScope.$emit("SHOW_IMAGE_SLIDER_EVENT", {index:index, images:photoUrls});
            }else{
                if(inc>0)
                    setTimeout(function(){
                        $rootScope.$emit("SHOW_IMAGE_SLIDER_EVENT", {index:index, images:photoUrls});
                    },10)
                else
                    $rootScope.$emit("SHOW_IMAGE_SLIDER_EVENT", {index:index, images:photoUrls});
                inc++
            }
        };
    }]);

app.controller('DescriptionController',['$scope', '$rootScope', '$routeParams', '$location', 'globalVariableService',
    function($scope, $rootScope, $routeParams, $location,globalVariableService){
        globalVariableService.initRootScope();
        $rootScope.showFooter = true;
        $rootScope.glbFooterType = glbFooterType.myInfoNext;
        $rootScope.pageTitle="我的资料";
        $routeParams.backUrl = '/myInfoDetails/myInfo';
}]);

app.controller('NewFriendsController', ['$scope', '$rootScope', '$location', 'globalVariableService','userService','$modal',
    function($scope, $rootScope, $location,globalVariableService, userService,$modal){
        globalVariableService.initRootScope();
        $rootScope.pageTitle="新的朋友";
        //获取好友请求
        userService.getFriendRequest().then(function(data){
            if(data.status == 0){
                if(data.data.friendReqs.length)
                {
                    $scope.friendArray = data.data.friendReqs;
                    for(var item in $scope.friendArray)
                    {
                        if($scope.friendArray[item].process_result == 1)
                        {
                            $scope.friendArray[item].isAdd = true;
                        }else if($scope.friendArray[item].process_result == 0){
                            $scope.friendArray[item].isAdd = false;
                        }
                        var req = $scope.friendArray[item].constellation;
                        if(req!=null)
                        {
                            $scope.friendArray[item].constellation = conste[req].name;
                        }
                    }
                }else{
                    var modalInstance = $modal.open({
                        templateUrl: 'views/tmpl_modal_alert.html',
                        controller: ModalAlertCtrl,
                        size: 'sm',
                        resolve: {
                            alertObj: function () {
                                return {message: "您还没有好友"};
                            }
                        }
                    });
                }
            }else{
                var modalInstance = $modal.open({
                    templateUrl: 'views/tmpl_modal_alert.html',
                    controller: ModalAlertCtrl,
                    size: 'sm',
                    resolve: {
                        alertObj: function () {
                            return {message: data.friendlyMessage};
                        }
                    }
                });
            }
        });

        $scope.checkDetails = function(index)
        {
            globalVariableService.userId = $scope.friendArray[index].user_id;
            $location.path("/detailsInfo").search({backUrl:"/friends"});
        }

        $scope.acceptReq = function (index) {
            //接受好友请求
            var applyUserId = {applyUserId: $scope.friendArray[index].user_id};
            userService.processFriendRequest(applyUserId).then(function(data){
                if(data.status == 0){
                    $scope.friendArray[index].isAdd = true;//切换到已添加
                    $scope.friendArray[index].summary = '';//申请好友下面的留言不显示
                }else{
                    var modalInstance = $modal.open({
                        templateUrl: 'views/tmpl_modal_alert.html',
                        controller: ModalAlertCtrl,
                        size: 'sm',
                        resolve: {
                            alertObj: function () {
                                return {message: data.friendlyMessage};
                            }
                        }
                    });
                }
            });
        }
    }]);

app.controller('TopicDetailsController', ['$scope', '$rootScope', '$location','globalVariableService',
    function($scope, $rootScope, $location,globalVariableService){
        $rootScope.pageTitle="";
        globalVariableService.initRootScope();
        $rootScope.showFooter = true;
    }]);

app.controller('AttestationController', ['$scope', '$rootScope','$routeParams', '$location','globalVariableService','$modal',
    function($scope, $rootScope, $routeParams, $location,globalVariableService,$modal){
        globalVariableService.initRootScope();
        $rootScope.pageTitle="我的资料";
        $routeParams.backUrl = '/myPersonInfo/myInfo/description';
        $rootScope.showFooter = true;
        $rootScope.glbFooterType = glbFooterType.myInfoNext;
        $rootScope.nextTxt = "完成";
        $scope.attes = {};
        $rootScope.canRemoveAction = false;
        $scope.divWidth = 0;
        $scope.uploadIdCar = false;
        if($rootScope.personInfo.identity_photo){
            $scope.divWidth = $rootScope.personInfo.identity_photo.length*65;
        }else{
            $rootScope.personInfo.identity_photo=[];
        }
       /* document.removeEventListener("FILE_UPLOADED_EVENT");
         document.addEventListener("FILE_UPLOADED_EVENT", function (evt) {
         $rootScope.isPending = false;
         $scope.isUploadSuccess = true;
         if($scope.uploadIdCar){
         $rootScope.personInfo.identity_photo.push(evt.data.url_320_240);
         $scope.divWidth = 65*($rootScope.personInfo.identity_photo.length);
         }else{
         $rootScope.personInfo.edu_bg_photo = evt.data.url_320_240;
         }
         $rootScope.$$phase || $rootScope.$apply();
         $scope.$$phase || $scope.$apply();
         }, false);*/
        var myInfohand = function(evt){
            $rootScope.isPending = false;
            $scope.isUploadSuccess = true;
            if($scope.uploadIdCar){
                $rootScope.personInfo.identity_photo.push(evt.data.url_320_240);
                $scope.divWidth = 65*($rootScope.personInfo.identity_photo.length);
            }else{
                $rootScope.personInfo.edu_bg_photo = evt.data.url_320_240;
            }
            $rootScope.$$phase || $rootScope.$apply();
            $scope.$$phase || $scope.$apply();
        }
        $scope.$on("$destroy",function(){
            document.removeEventListener("FILE_UPLOADED_EVENT", myInfohand, false);
        });
        document.addEventListener("FILE_UPLOADED_EVENT", myInfohand, false);


       /* document.removeEventListener("FILE_UPLOAD_CANCEL_EVENT");
        document.addEventListener("FILE_UPLOAD_CANCEL_EVENT", function (evt) {
            $rootScope.isPending = false;
            $rootScope.$$phase || $rootScope.$apply();
        }, false);*/
        var cancelHand = function(evt){
            $rootScope.isPending = false;
            $rootScope.$$phase || $rootScope.$apply();
        }
        $scope.$on("$destroy",function(){
            document.removeEventListener("FILE_UPLOAD_CANCEL_EVENT", cancelHand, false);
        });
        document.addEventListener("FILE_UPLOAD_CANCEL_EVENT", cancelHand, false);

        //上传身份证
        $scope.uploadIdCard = function()
        {
            $scope.uploadIdCar = true;
            $rootScope.isPending = true;
            $scope.isUploadSuccess = false;
            var params = {uploadImgType: 3};
            if (browser.versions.ios) {
                Log.log("try to call ios native to add image");
                if (window.WebViewJavascriptBridge) {
                    WebViewJavascriptBridge.callHandler('uploadImage', params, function (response) {
                        Log.log("call native to upload image");
                    });
                }
            } else if (browser.versions.android) {
                Log.log("try to call android native to add image");
                jsb.require('uploadImage', params, function (response) {
                    Log.log("call native to upload image");
                });
            }
        };
        //上传毕业证
        $scope.uploadEducation = function()
        {
            $scope.uploadIdCar = false;
            globalVariableService.uploadImage($scope, 4);
        }

}]);

app.controller('ProfileController', ['$scope', '$rootScope', '$routeParams', '$location','globalVariableService','$modal',
    function($scope, $rootScope, $routeParams, $location,globalVariableService,$modal){
        globalVariableService.initRootScope();
        $rootScope.pageTitle="我的资料";
        $routeParams.backUrl = '/myPersonInfo/myInfo/description/attestation';
        $rootScope.showFooter = true;
        $rootScope.glbFooterType = glbFooterType.myInfoNext;
        $rootScope.nextTxt = "确认提交";

}]);

app.controller('ExamineStateController',['$scope', '$rootScope', '$location','globalVariableService','$modal',
    function($scope, $rootScope, $location,globalVariableService,$modal){
        globalVariableService.initRootScope();
        $rootScope.pageTitle="我的资料";
        $scope.anewWrite = function()
        {
            $location.path('/myPersonInfo/myInfo').search({backUrl:'/myPersonInfo'});
        }

}]);

app.controller('PayController', [ '$scope', '$rootScope', '$location','globalVariableService','$modal','userService',
    function($scope, $rootScope, $location,globalVariableService,$modal,userService){
        globalVariableService.initRootScope();
        $rootScope.pageTitle="支付";
        $scope.payment = {pitch: 2};
        $scope.changeCheck = function(type)
        {
            $scope.payment.pitch = type;
        }
        //获取VIP套餐
        userService.getVipPlan().then(function(data){
            if(data.status == 0){
                $scope.planId = data.data.plans[0].PlanId;
            }else{
                var modalInstance = $modal.open({
                    templateUrl: 'views/tmpl_modal_alert.html',
                    controller: ModalAlertCtrl,
                    size: 'sm',
                    resolve: {
                        alertObj: function () {
                            return {message: data.friendlyMessage};
                        }
                    }
                });
            }
        })
        //确认支付
        $scope.confirmPay = function () {
            var params = {planId: $scope.planId};
            userService.buyPlan(params).then(function(data){
                if(data.status == 0 && data.data){
                    var params = {orderNum: data.data.orderNum, orderType:'', payType:$scope.payment.pitch, paySuccessUrl:'SuccessVIP', payErrorUrl:'PayFails', authToken:getAuthToken()};
                    if (browser.versions.ios) {
                        Log.log("try to call ios native to add image");
                        if (window.WebViewJavascriptBridge) {
                            WebViewJavascriptBridge.callHandler('pay', params, function (response) {
                                Log.log("call native to upload image");
                            });
                        }
                    } else if (browser.versions.android) {
                        Log.log("try to call android native to add image");
                        jsb.require('pay', params, function (response) {
                            Log.log("call native to upload image");
                        });
                    }
                }
            });
        }
    }]);

app.controller('BecomeVipController', ['$scope', '$rootScope','$routeParams', '$location','globalVariableService',
    function($scope, $rootScope, $routeParams, $location,globalVariableService){
        globalVariableService.initRootScope();
        $rootScope.pageTitle = '成为VIP';
        $routeParams.backUrl = '/myPersonInfo';
        $scope.becomeVip = function () {
            $location.path('/myPersonInfo/pay').search({backUrl:'/becomeVIP'});
        }
 }]);

app.controller('UserLoginController', ['$scope', '$rootScope','$routeParams', '$location','globalVariableService','$modal','userService',
    function($scope, $rootScope, $routeParams, $location,globalVariableService,$modal,userService){
        globalVariableService.initRootScope();
        $rootScope.pageTitle = '会员登录';
        $scope.login = function()
        {
            var loginInfo = {
                'phoneNum': $scope.phoneNum,
                'password': $scope.password
                                };
            var a =  /^1\d{10}$/;
            if(loginInfo.phoneNum == undefined)
            {
                var modalInstance = $modal.open({
                    templateUrl: 'views/tmpl_modal_alert.html',
                    controller: ModalAlertCtrl,
                    size: 'sm',
                    resolve: {
                        alertObj: function(){
                            return {message: '请输入手机号'};
                        }
                    }
                });
                return;
            }
            else if(loginInfo.phoneNum.length != 11 || !loginInfo.phoneNum.match(a))
            {
                var modalInstance = $modal.open({
                    templateUrl: 'views/tmpl_modal_alert.html',
                    controller: ModalAlertCtrl,
                    size: 'sm',
                    resolve: {
                        alertObj: function () {
                            return {message: '请输入正确的手机号码'};
                        }
                    }
                });
                return;
            }
            if(loginInfo.password == undefined)
            {
                var modalInstance = $modal.open({
                    templateUrl: 'views/tmpl_modal_alert.html',
                    controller: ModalAlertCtrl,
                    size:'sm',
                    resolve: {
                        alertObj: function(){
                            return {message: '请输入密码'};
                        }
                    }
                });
                return;
            }
            userService.login(loginInfo).then(function(data){
                if (data.status == 0) {
                    setAuthToken(data.data.token);
                    setSnsToken(data.data.snsToken);
                    userService.getProfile().then(function(data){
                        if(data.status == 0){
                            if(data.data.user_status == 0 ){
                                /*modalMessage($modal, '您还没有注册,请您注册');*/
                                $location.path("/promptRegister").search({backUrl: '/'});

                            }
                            if(data.data.user_status == 1){
                                /*modalMessage($modal, '您的资料还在审核中,请耐心等待');*/
                                globalVariableService.userInfo = data.data;
                                $rootScope.sexIndex = data.data.sex;
                                $rootScope.constellationIndex = data.data.constellation;
                                $rootScope.education_backgroundIndex = data.data.education_background;
                                $location.path("/promptExamine").search({backUrl: '/'});
                              /*  $location.path("/");*/
                            }
                            if(data.data.user_status == 2){
                                /*modalMessage($modal, '您的资料已通过审核');*/
                                globalVariableService.userInfo = data.data;
                                $rootScope.sexIndex = data.data.sex;
                                $rootScope.constellationIndex = data.data.constellation;
                                $rootScope.education_backgroundIndex = data.data.education_background;
                                $location.path("/");
                            }
                            if(data.data.user_status == 3){
                                /*modalMessage($modal, '您的资料没有通过审核，原因是有所填内容不符合,请重新注册');*/
                                $location.path("/promptFeedback").search({backUrl: '/'});
                            }
                        }else{
                            var modalInstance = $modal.open({
                                templateUrl: 'views/tmpl_modal_alert.html',
                                controller: ModalAlertCtrl,
                                size: 'sm',
                                resolve: {
                                    alertObj: function () {
                                        return {message: data.friendlyMessage};
                                    }
                                }
                            });
                        }
                    });
                    var internal = setTimeout(function(){
                        //如果是 ios 设备，把用户登录的 token 传入到原生里面保存，用于上传图片的 token 验证
                        /*alert("internalLogin: "+ data.data.token + data.data.snsToken);*/
                        internalLogin(data.data.token, data.data.snsToken);
                    }, 2000);
                    /*alert('hxUserName:'+ data.data.hxUserName+ ' hxPassWord:'+ data.data.hxPassWord);*/
                    hxLogin(data.data.hxUserName, data.data.hxPassWord);
                } else {
                    setAuthToken(null);
                    var modalInstance = $modal.open({
                        templateUrl: 'views/tmpl_modal_alert.html',
                        controller: ModalAlertCtrl,
                        size: 'sm',
                        resolve: {
                            alertObj: function () {
                                return {message: data.friendlyMessage};
                            }
                        }
                    });
                }
            });
        }
        $scope.register = function()
        {
            $location.path("/user/register").search({backUrl: "/user/login"});
        }
        $scope.findPassWord = function()
        {
            $location.path("/user/findPassWord").search({backUrl: "/user/login"});
        }
    }]);

app.controller('UserRegisterController', ['$scope', '$rootScope','$routeParams', '$location','globalVariableService','$modal','userService',
    function($scope, $rootScope, $routeParams, $location,globalVariableService, $modal,userService){
        globalVariableService.initRootScope();
        $rootScope.pageTitle = '注册';
        $rootScope.set = {};
        $scope.GetShortMsgCode = function()
        {
            var a =  /^1\d{10}$/;
            if($rootScope.set.phoneNum == undefined)
            {
                var modalInstance = $modal.open({
                    templateUrl: 'views/tmpl_modal_alert.html',
                    controller: ModalAlertCtrl,
                    size: 'sm',
                    resolve: {
                        alertObj: function(){
                            return {message: '请输入手机号'};
                        }
                    }
                });
                return;
            }
            else if($rootScope.set.phoneNum.length != 11 ||! $rootScope.set.phoneNum.match(a))
            {
                var modalInstance = $modal.open({
                    templateUrl: 'views/tmpl_modal_alert.html',
                    controller: ModalAlertCtrl,
                    size: 'sm',
                    resolve: {
                        alertObj: function () {
                            return {message: '请输入正确的手机号码！'};
                        }
                    }
                });
                return;
            }else{
                var num = 60;
                $('shortMsgCode').css('color', '#999999').attr('disabled', 'disabled');
                var interId = setInterval(function(){
                    document.getElementById("shortMsgCode").innerText = num--+'秒后重发';
                    if(num <= 0)
                    {
                        $('shortMsgCode').attr('disabled', false);
                        $('shortMsgCode').text('获取验证码').css('color', '#62D5DA');
                        clearInterval(interId);
                    }
                }, 1000);
                var  info = {
                    phoneNum: $rootScope.set.phoneNum,
                    behavior: 1
                };
                userService.getShortMsg(info).then(function(data){
                    if (data.status == 0) {
                    }
                    else {
                        var modalInstance = $modal.open({
                            templateUrl: 'views/tmpl_modal_alert.html',
                            controller: ModalAlertCtrl,
                            size: 'sm',
                            resolve: {
                                alertObj: function () {
                                    return {message: data.friendlyMessage};
                                }
                            }
                        });
                    }
                });
            }
        }
        $scope.nextStep = function()
        {
            var a =  /^1\d{10}$/;
            if($rootScope.set.phoneNum == undefined)
            {
                var modalInstance = $modal.open({
                    templateUrl: 'views/tmpl_modal_alert.html',
                    controller: ModalAlertCtrl,
                    size: 'sm',
                    resolve: {
                        alertObj: function(){
                            return {message: '请输入手机号'};
                        }
                    }
                });
                return;
            }
            else if($rootScope.set.phoneNum.length != 11 || !$rootScope.set.phoneNum.match(a))
            {
                var modalInstance = $modal.open({
                    templateUrl: 'views/tmpl_modal_alert.html',
                    controller: ModalAlertCtrl,
                    size: 'sm',
                    resolve: {
                        alertObj: function () {
                            return {message: '请输入正确的手机号码！'};
                        }
                    }
                });
                return;
            }
            if($rootScope.set.shortMsgCode == undefined)
            {
                var modalInstance = $modal.open({
                    templateUrl: 'views/tmpl_modal_alert.html',
                    controller: ModalAlertCtrl,
                    size: 'sm',
                    resolve: {
                        alertObj: function () {
                            return {message: '请输入验证码！'};
                        }
                    }
                });
                return;
            }
            $location.path("/user/register/complete").search({backUrl: "/user/register"});
        }
    }]);

app.controller('CompleteController', ['$scope', '$rootScope','$routeParams', '$location','globalVariableService','$modal','userService',
    function($scope, $rootScope, $routeParams, $location,globalVariableService, $modal,userService){
        $rootScope.pageTitle = '注册';
        $scope.complete = function()
        {
            if($rootScope.set.password == undefined)
            {
                var modalInstance = $modal.open({
                    templateUrl: 'views/tmpl_modal_alert.html',
                    controller: ModalAlertCtrl,
                    size: 'sm',
                    resolve: {
                        alertObj: function(){
                            return {message: '请输入密码'};
                        }
                    }
                });
                return;
            }
            if($rootScope.set.confirmPassword == undefined)
            {
                var modalInstance = $modal.open({
                    templateUrl: 'views/tmpl_modal_alert.html',
                    controller: ModalAlertCtrl,
                    size: 'sm',
                    resolve: {
                        alertObj: function(){
                            return {message: '请输入确认密码'};
                        }
                    }
                });
                return;
            }
            if($rootScope.set.password != $rootScope.set.confirmPassword)
            {
                var modalInstance = $modal.open({
                    templateUrl: 'views/tmpl_modal_alert.html',
                    controller: ModalAlertCtrl,
                    size: 'sm',
                    resolve: {
                        alertObj: function(){
                            return {message: '两次密码输入不一致'};
                        }
                    }
                });
                return;
            }
            var registerInfo = {
                phoneNum: $rootScope.set.phoneNum,
                shortMsgCode: $rootScope.set.shortMsgCode,
                password: $rootScope.set.password,
                confirmPassword: $rootScope.set.confirmPassword
            };
            userService.userRegister(registerInfo).then(function(data){
                if (data.status == 0) {
                    $rootScope.registerBack = true;
                    setAuthToken(data.data.token);
                    setSnsToken(data.data.snsToken);
                    internalLogin(data.data.token, data.data.snsToken);
                    $location.path('/loadingHead');
                    /*$location.path("/");*/
                }
                else {
                    setAuthToken(null);
                    var modalInstance = $modal.open({
                        templateUrl: 'views/tmpl_modal_alert.html',
                        controller: ModalAlertCtrl,
                        size: 'sm',
                        resolve: {
                            alertObj: function () {
                                return {message: data.friendlyMessage};
                            }
                        }
                    });
                }
            });
        }
    }]);

app.controller('FindPassWordController', ['$scope', '$rootScope','$routeParams', '$location','globalVariableService','$modal','userService',
    function($scope, $rootScope, $routeParams, $location,globalVariableService,$modal,userService){
        $rootScope.pageTitle = '找回密码';
        $scope.GetShortMsgCode = function()
        {
            var a =  /^1\d{10}$/;
            if($scope.phoneNum == undefined)
            {
                var modalInstance = $modal.open({
                    templateUrl: 'views/tmpl_modal_alert.html',
                    controller: ModalAlertCtrl,
                    size: 'sm',
                    resolve: {
                        alertObj: function(){
                            return {message: '请输入手机号'};
                        }
                    }
                });
                return;
            }
            else if($scope.phoneNum.length != 11 || !$scope.phoneNum.match(a))
            {
                var modalInstance = $modal.open({
                    templateUrl: 'views/tmpl_modal_alert.html',
                    controller: ModalAlertCtrl,
                    size: 'sm',
                    resolve: {
                        alertObj: function () {
                            return {message: '请输入正确的手机号码！'};
                        }
                    }
                });
                return;
            }else{
                var num = 60;
                $('shortMsgCode').css('color', '#999999').attr('disabled', 'disabled');
                var interId = setInterval(function(){
                    document.getElementById("shortMsgCode").innerText = num--+'秒后重发';
                    if(num <= 0)
                    {
                        $('shortMsgCode').attr('disabled', false);
                        $('shortMsgCode').text('获取验证码').css('color', '#62D5DA');
                        clearInterval(interId);
                    }
                }, 1000);
            }
            var info = {phoneNum: $scope.phoneNum, behavior: 2};
            userService.getShortMsg(info).then(function(data){
                if (data.status == 0) {
                }
                else {
                    var modalInstance = $modal.open({
                        templateUrl: 'views/tmpl_modal_alert.html',
                        controller: ModalAlertCtrl,
                        size: 'sm',
                        resolve: {
                            alertObj: function () {
                                return {message: data.friendlyMessage};
                            }
                        }
                    });
                }
            });
        }
        $scope.findPassWord = function()
        {
            var a =  /^1\d{10}$/;
            if($scope.phoneNum == undefined)
            {
                var modalInstance = $modal.open({
                    templateUrl: 'views/tmpl_modal_alert.html',
                    controller: ModalAlertCtrl,
                    size: 'sm',
                    resolve: {
                        alertObj: function(){
                            return {message: '请输入手机号'};
                        }
                    }
                });
                return;
            }
           if($scope.phoneNum.length != 11 || !$scope.phoneNum.match(a))
            {
                var modalInstance = $modal.open({
                    templateUrl: 'views/tmpl_modal_alert.html',
                    controller: ModalAlertCtrl,
                    size: 'sm',
                    resolve: {
                        alertObj: function () {
                            return {message: '请输入正确的手机号码！'};
                        }
                    }
                });
                return;
            }
            if($scope.codeNum == undefined)
            {
                var modalInstance = $modal.open({
                    templateUrl: 'views/tmpl_modal_alert.html',
                    controller: ModalAlertCtrl,
                    size: 'sm',
                    resolve: {
                        alertObj: function(){
                            return {message: '请输入验证码'};
                        }
                    }
                });
                return;
            }
            if($scope.passWord == undefined)
            {
                var modalInstance = $modal.open({
                    templateUrl: 'views/tmpl_modal_alert.html',
                    controller: ModalAlertCtrl,
                    size: 'sm',
                    resolve: {
                        alertObj: function(){
                            return {message: '请输入新密码'};
                        }
                    }
                });
                return;
            }
            if($scope.newPassWord == undefined)
            {
                var modalInstance = $modal.open({
                    templateUrl: 'views/tmpl_modal_alert.html',
                    controller: ModalAlertCtrl,
                    size: 'sm',
                    resolve: {
                        alertObj: function(){
                            return {message: '请输入确认新密码'};
                        }
                    }
                });
                return;
            }
            if($scope.newPassWord != $scope.passWord)
            {
                var modalInstance = $modal.open({
                    templateUrl: 'views/tmpl_modal_alert.html',
                    controller: ModalAlertCtrl,
                    size: 'sm',
                    resolve: {
                        alertObj: function(){
                            return {message: '您输入的两次密码不一样，请重新输入'};
                        }
                    }
                });
                return;
            }
            var findPassWordInfo = {
                'phoneNum': $scope.phoneNum,
                'shortMsgCode': $scope.codeNum,
                'password': $scope.passWord
            };
            userService.findPassWord(findPassWordInfo).then(function(data){
                if(data.status == 0){
                    $location.path('/');
                }else{
                    var modalInstance = $modal.open({
                        templateUrl: 'views/tmpl_modal_alert.html',
                        controller: ModalAlertCtrl,
                        size: 'sm',
                        resolve: {
                            alertObj: function () {
                                return {message: data.friendlyMessage};
                            }
                        }
                    });
                }
            })
        }

    }]);

app.controller('AlreadyVIPController', [ '$scope', '$rootScope', '$location','globalVariableService','$modal',
    function($scope, $rootScope, $location,globalVariableService,$modal){
        globalVariableService.initRootScope();
        $rootScope.pageTitle='VIP会员';
        $scope.alreadyVip = function()
        {
            $location.path('/myPersonInfo');
        }

    }]);

app.controller('LoadingHeadController', ['$scope', '$rootScope', '$location','globalVariableService','$modal',
    function($scope, $rootScope, $location,globalVariableService,$modal){
        globalVariableService.initRootScope();
        $rootScope.pageTitle = 'FashionSingles';
        $scope.cancel = function()
        {
            $location.path('/myInfoDetails/myInfo');
        }
        $scope.agreementNext = function()
        {
            $location.path('/myPersonInfo/myAbout/agreement').search({backUrl: '/loadingHead'});
        }
    }]);

app.controller('IntegralController', ['$scope', '$rootScope', '$location','globalVariableService','$modal','userService',
    function($scope, $rootScope, $location,globalVariableService,$modal, userService){
        globalVariableService.initRootScope();
        $rootScope.pageTitle = '我的积分';
        $scope.score = 0;
        userService.getScore().then(function(data){
            if(data.status == 0){
                $scope.score = data.data.score;
            }else{
                var modalInstance = $modal.open({
                    templateUrl: 'views/tmpl_modal_alert.html',
                    controller: ModalAlertCtrl,
                    size: 'sm',
                    resolve: {
                        alertObj: function () {
                            return {message: data.friendlyMessage};
                        }
                    }
                });
            }
        });

        $scope.toShare = function()
        {
            $location.path('/myPersonInfo/integral/shareFriends').search({backUrl: '/myPersonInfo/integral'});
        };

        $scope.playShare = function()
        {
            $location.path("/playIntegral").search({backUrl: '/myPersonInfo/integral'});
        }

        $scope.updateVIP = function(){
            //获取用户是不是会员
            userService.getRole().then(function(data){
                if(data.status == 0){
                    if(data.data.role == 2){
                        $location.path('/alreadyVIP').search({backUrl: '/myPersonInfo/integral'});
                    }else{
                        $location.path('/becomeVIP').search({backUrl: '/myPersonInfo/integral'});
                    }
                }else{
                    var modalInstance = $modal.open({
                        templateUrl: 'views/tmpl_modal_alert.html',
                        controller: ModalAlertCtrl,
                        size: 'sm',
                        resolve: {
                            alertObj: function () {
                                return {message: data.friendlyMessage};
                            }
                        }
                    });
                }
            });
        }
    }]);

app.controller('ShareFriendsController', ['$scope', '$rootScope', '$location','globalVariableService','$modal','userService',
    function($scope, $rootScope, $location,globalVariableService,$modal,userService){
        globalVariableService.initRootScope();
        $rootScope.pageTitle = '分享';
        var hand = function (evt) {
            $rootScope.isPending = false;
            if(evt.data.shareInfoStatus === '1') {
                //分享成功后加积分
                userService.shareInfo();
            }
            $rootScope.$$phase || $rootScope.$apply();
            $scope.$$phase || $scope.$apply();
        };
        $scope.$on("$destroy",function(){
            document.removeEventListener("SHARE_INFO_CALLBACK", hand, false);
        });
        document.addEventListener("SHARE_INFO_CALLBACK", hand, false);


        $scope.shareSnsFriends =function() {
            shareInfo(1);
        }
        $scope.shareSnsO = function() {
            shareInfo(2);
        }
        $scope.cancelShare = function(){
            $location.path('/myPersonInfo/integral');
        }

    }]);

app.controller('changePasswordController', ['$scope', '$rootScope', '$location','globalVariableService','$modal','userService',
    function($scope, $rootScope, $location,globalVariableService,$modal,userService){
        globalVariableService.initRootScope();
        $rootScope.pageTitle = "修改密码";
        $scope.revisePassword = function()
        {
            if($scope.oldPassword == undefined)
            {
                var modalInstance = $modal.open({
                    templateUrl: 'views/tmpl_modal_alert.html',
                    controller: ModalAlertCtrl,
                    size: 'sm',
                    resolve: {
                        alertObj: function(){
                            return {message: '请输入旧密码'};
                        }
                    }
                });
                return;
            }
            if($scope.password == undefined)
            {
                var modalInstance = $modal.open({
                    templateUrl: 'views/tmpl_modal_alert.html',
                    controller: ModalAlertCtrl,
                    size: 'sm',
                    resolve: {
                        alertObj: function(){
                            return {message: '请输入新密码'};
                        }
                    }
                });
                return;
            }
            if($scope.newPassword == undefined)
            {
                var modalInstance = $modal.open({
                    templateUrl: 'views/tmpl_modal_alert.html',
                    controller: ModalAlertCtrl,
                    size: 'sm',
                    resolve: {
                        alertObj: function(){
                            return {message: '请输入确认新密码'};
                        }
                    }
                });
                return;
            }
            if($scope.newPassword != $scope.password){
                var modalInstance = $modal.open({
                    templateUrl: 'views/tmpl_modal_alert.html',
                    controller: ModalAlertCtrl,
                    size: 'sm',
                    resolve: {
                        alertObj: function(){
                            return {message: '请确认两次新密码输入正确'};
                        }
                    }
                });
                return;
            }
            var params = {oldPassword: $scope.oldPassword, password: $scope.password};
            userService.changePassword(params).then(function(data){
                if(data.status == 0){
                    var modalInstance = $modal.open({
                        templateUrl: 'views/tmpl_modal_alert.html',
                        controller: ModalAlertCtrl,
                        size: 'sm',
                        resolve: {
                            alertObj: function () {
                                return {message: '修改密码成功'};
                            }
                        }
                    });
                    $location.path('/myPersonInfo');
                }else{
                    var modalInstance = $modal.open({
                        templateUrl: 'views/tmpl_modal_alert.html',
                        controller: ModalAlertCtrl,
                        size: 'sm',
                        resolve: {
                            alertObj: function () {
                                return {message: data.friendlyMessage};
                            }
                        }
                    });
                }
            });
        }

    }]);

app.controller('PlayIntegralController', ['$scope', '$rootScope', '$location','globalVariableService','$modal','userService',
        function($scope, $rootScope, $location,globalVariableService,$modal,userService){
        globalVariableService.initRootScope();
        $rootScope.pageTitle = '购买积分';
        $scope.integralArray = [];
        $scope.play = {};
         userService.getScorePlan().then(function(data){
             if(data.status == 0){
                $scope.integralArray = data.data.plans;
                $scope.play = data.data.plans[0];
             }else{
                 var modalInstance = $modal.open({
                     templateUrl: 'views/tmpl_modal_alert.html',
                     controller: ModalAlertCtrl,
                     size: 'sm',
                     resolve: {
                         alertObj: function () {
                             return {message: data.friendlyMessage};
                         }
                     }
                 });
             }
         });

        $scope.playIntegral = function()
        {
            $rootScope.Score = $scope.play.Score;
            $location.path('/playScore').search({backUrl: '/playIntegral', planId:  $scope.play.PlanId});
        }

    }]);

app.controller('PromptRegisterController', ['$scope', '$rootScope', '$location','globalVariableService','$modal','userService',
    function($scope, $rootScope, $location,globalVariableService,$modal,userService){
        globalVariableService.initRootScope();
        $rootScope.pageTitle = '注册';
        $scope.checkInfo = function()
        {
            $location.path("/loadingHead");
        }
    }]);

app.controller('PromptExamineController', ['$scope', '$rootScope', '$location','globalVariableService','$modal','userService',
    function($scope, $rootScope, $location,globalVariableService,$modal,userService){
        globalVariableService.initRootScope();
        $rootScope.pageTitle = '审核';
        $scope.checkInfo = function()
        {
           /* $location.path("/");*/
            $location.path("/loadingHead");
        }

    }]);

app.controller('PromptFeedbackController', ['$scope', '$rootScope', '$location','globalVariableService','$modal','userService',
    function($scope, $rootScope, $location,globalVariableService,$modal,userService){
        globalVariableService.initRootScope();
        $rootScope.pageTitle = '审核反馈';
        $scope.status = {};
        /*$scope.status.remark = "您提交的信息不符合"*/
        userService.getProfile().then(function(data){
           if(data.status == 0){
               if(data.data.user_status == 3){
                   $scope.status.remark = data.data.remark;
               }
           }else{
               var modalInstance = $modal.open({
                   templateUrl: 'views/tmpl_modal_alert.html',
                   controller: ModalAlertCtrl,
                   size: 'sm',
                   resolve: {
                       alertObj: function () {
                           return {message: data.friendlyMessage};
                       }
                   }
               });
           }
        });
        $scope.feedbackInfo = function()
        {
            $location.path("/loadingHead");
        }
    }]);

app.controller('IntegralPromptController', ['$scope', '$rootScope', '$location','globalVariableService','$modal','userService',
    function($scope, $rootScope, $location,globalVariableService,$modal,userService){
        globalVariableService.initRootScope();
        $scope.searchList = true;
        $rootScope.pageTitle = '积分不足';

        //升级成VIP会员
        $scope.updateScore = function()
        {
            $location.path('/becomeVIP').search({backUrl:"/integralPrompt"});
        }
        //挣取积分
        $scope.earnPoints = function()
        {
            $location.path("/myPersonInfo/integral").search({backUrl:"/integralPrompt"});
        }
        userService.getLastSearch().then(function(data){
            if(data.status == 0){
                $scope.searchFriendAry = data.data.friends;
                if(data.data.friends.length == 0){
                    $scope.searchList = false;
                }
                for(var item in $scope.searchFriendAry){
                    var req = $scope.searchFriendAry[item].constellation;
                    if(req!=null)
                    {
                        $scope.searchFriendAry[item].constellation = conste[req].name;
                    }
                }
            }else{
                var modalInstance = $modal.open({
                    templateUrl: 'views/tmpl_modal_alert.html',
                    controller: ModalAlertCtrl,
                    size: 'sm',
                    resolve: {
                        alertObj: function () {
                            return {message: data.friendlyMessage};
                        }
                    }
                });
            }
        });

        $scope.checkDetails = function(index){
            $location.path("/detailsInfo").search({backUrl:"/search", user_id: $scope.searchFriendAry[index].userId});
        }

    }]);

app.controller('AboutController', ['$scope', '$rootScope', '$location','globalVariableService','$modal','userService',
    function($scope, $rootScope, $location,globalVariableService,$modal,userService){
        globalVariableService.initRootScope();
        $rootScope.pageTitle = '关于我们';
        $scope.comeBack = function()
        {
            $location.path("/myPersonInfo");
        }
    }]);

app.controller('AgreementController', ['$scope', '$rootScope', '$location','globalVariableService','$modal','userService',
    function($scope, $rootScope, $location,globalVariableService,$modal,userService){
        globalVariableService.initRootScope();
        $rootScope.pageTitle = '用户协议和隐私声明';
        $scope.userAgreement = function()
        {
            $location.path("/loadingHead");
        }

    }]);

app.controller('searchFriendsController', ['$scope', '$rootScope', '$location','globalVariableService','$modal','userService',
    function($scope, $rootScope, $location,globalVariableService,$modal,userService){
        globalVariableService.initRootScope();
        $rootScope.pageTitle = '搜索列表';
      /*  $scope.everySearchCount = true;*/
        $scope.friendsArr = globalVariableService.friendsArray;
        for(var i=0; i<$scope.friendsArr.length; i++){
            var req = $scope.friendsArr[i].constellation;
            if(!isNaN(req)){
                $scope.friendsArr[i].constellation = conste[req].name;
            }
        }
        $scope.checkDetails = function(index)
        {
            globalVariableService.userId = $scope.friendsArr[index].userId;
            //globalVariableService.friendDetailsInfo = $scope.friendsArr[index];
            $location.path("/detailsInfo").search({backUrl:"/searchFriends"});
        }
    }]);

app.controller('PlayScoreController', ['$scope', '$rootScope', '$location','globalVariableService','$modal','userService','$routeParams',
    function($scope, $rootScope, $location,globalVariableService,$modal,userService,$routeParams){
        globalVariableService.initRootScope();
        $rootScope.pageTitle = '购买积分';
        var planId=$routeParams.planId;
        //默认选择微信支付
        $scope.payment = {pitch: 2};
        $scope.changeCheck = function(type)
        {
            $scope.payment.pitch = type;
        }
        $scope.payGetScore = function(){
            var params = {planId: planId};
            userService.buyPlan(params).then(function(data){
                if(data.status == 0 && data.data){
                    var params = {orderNum: data.data.orderNum, orderType:'', payType:$scope.payment.pitch, paySuccessUrl:'SuccessPayIntegral', payErrorUrl:'PayFailsIntegral', authToken:getAuthToken()};
                    if (browser.versions.ios) {
                        Log.log("try to call ios native to add image");
                        if (window.WebViewJavascriptBridge) {
                            WebViewJavascriptBridge.callHandler('pay', params, function (response) {
                                Log.log("call native to upload image");
                            });
                        }

                    } else if (browser.versions.android) {
                        Log.log("try to call android native to add image");
                        jsb.require('pay', params, function (response) {
                            Log.log("call native to upload image");
                        });
                    }
                }
            });
        }
    }]);

app.controller('SuccessVIPController', ['$scope', '$rootScope', '$location','globalVariableService','$modal','userService',
    function($scope, $rootScope, $location,globalVariableService,$modal,userService){
        globalVariableService.initRootScope();
        $rootScope.pageTitle = '购买成功';
        $scope.determine = function()
        {
            $location.path('/');
        }

    }]);

app.controller('CollectController', ['$scope', '$rootScope', '$location','globalVariableService','$modal','userService',
    function($scope, $rootScope, $location,globalVariableService,$modal,userService){
        globalVariableService.initRootScope();
        $rootScope.pageTitle = '我的收藏';
        $scope.collectArr = {};
        userService.getCollects().then(function(data){
            if(data.status == 0){
                $scope.collectArr = data.data;
                for(var item in $scope.collectArr){
                    var req = $scope.collectArr[item].constellation;
                    if(req!=null)
                    {
                        $scope.collectArr[item].constellation = conste[req].name;
                    }
                }
            }else{
                var modalInstance = $modal.open({
                    templateUrl: 'views/tmpl_modal_alert.html',
                    controller: ModalAlertCtrl,
                    size: 'sm',
                    resolve: {
                        alertObj: function () {
                            return {message: data.friendlyMessage};
                        }
                    }
                });
            }
        });
        $scope.checkDetails=function(index){
            globalVariableService.userId = $scope.collectArr[index].userId;
            $location.path('/detailsInfo').search({backUrl:'/collect'});
        }
    }]);

app.controller('SearchCountFullController', ['$scope', '$rootScope', '$location','globalVariableService','$modal','userService',
    function($scope, $rootScope, $location,globalVariableService,$modal,userService){
        globalVariableService.initRootScope();
        $rootScope.pageTitle = '搜索';
        $scope.searchList = true;
        $scope.goToSns = function(){
            setExitSNSSiteUrl( location.href);
            location.href = './sns/index.html?snsEnv='+getStorageValue('snsEnv')+'&snsApiKey='+getStorageValue('snsApiKey')+'&snsToken='+getStorageValue('snsToken');
        }

        userService.getLastSearch().then(function(data){
            if(data.status == 0){
                $scope.historySearch = data.data.friends;
                if(data.data.friends.length == 0){
                    $scope.searchList = false;
                }
                for(var item in $scope.historySearch){
                    var req = $scope.historySearch[item].constellation;
                    if(!isNaN(req))
                    {
                        $scope.historySearch[item].constellation = conste[req].name;
                    }
                }
            }else{
                var modalInstance = $modal.open({
                    templateUrl: 'views/tmpl_modal_alert.html',
                    controller: ModalAlertCtrl,
                    size: 'sm',
                    resolve: {
                        alertObj: function () {
                            return {message: data.friendlyMessage};
                        }
                    }
                });
            }
        });

        $scope.checkDetails = function(index){
            globalVariableService.userId = $scope.historySearch[index].userId;
            $location.path("/detailsInfo").search({backUrl:"/searchCountFull"});
        }
    }]);

app.controller('SuccessPayIntegralController', ['$scope', '$rootScope', '$location','globalVariableService','$modal','userService','$routeParams',
    function($scope, $rootScope, $location,globalVariableService,$modal,userService,$routeParams){
        globalVariableService.initRootScope();
        $rootScope.pageTitle = '购买成功';
        $scope.IntegralNum = $rootScope.Score;
        $scope.determine = function()
        {
            $location.path('/');
        }

    }]);

app.controller('PayFailsVIPController', ['$scope', '$rootScope', '$location','globalVariableService','$modal','userService','$routeParams',
    function($scope, $rootScope, $location,globalVariableService,$modal,userService,$routeParams){
        globalVariableService.initRootScope();
        $rootScope.pageTitle = '未完成购买';
        $scope.determine = function()
        {
            $location.path('/myPersonInfo/pay');
        }

    }]);

app.controller('PayFailsIntegralController', ['$scope', '$rootScope', '$location','globalVariableService','$modal','userService','$routeParams',
    function($scope, $rootScope, $location,globalVariableService,$modal,userService,$routeParams){
        globalVariableService.initRootScope();
        $rootScope.pageTitle = '未完成购买';
        $scope.determine = function()
        {
            $location.path('/playIntegral');
        }

    }]);

app.controller('ShareSuccessController', ['$scope', '$rootScope', '$location','globalVariableService','$modal','userService','$routeParams',
    function($scope, $rootScope, $location,globalVariableService,$modal,userService,$routeParams){
        globalVariableService.initRootScope();
        $rootScope.pageTitle = '分享成功';
        $scope.shareAddIntegral = 5;
        $scope.determine = function()
        {
            $location.path('/');
        }

    }]);

//
var ModalCommonMsgCtrl = function ($scope, $rootScope, $modalInstance, $modal, snsService, obj) {
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
    $scope.confirm = function (callback) {
        $modalInstance.dismiss('cancel');
        if (callback)
            obj.scope.$emit(callback, obj);
    };
    $scope.obj = obj;
};